/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.utl.ist.isr.livetrack.ui;

import java.awt.Image;
import java.awt.event.ActionEvent;
import javax.swing.Action;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.PropertySupport;
import org.openide.nodes.PropertySupport.Reflection;
import org.openide.nodes.Sheet;
import org.openide.util.Exceptions;
import org.openide.util.HelpCtx;
import org.openide.util.ImageUtilities;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;
import org.openide.util.actions.SystemAction;
import org.openide.util.lookup.Lookups;
import pt.utl.ist.isr.livetrack.server.LTServer;
import pt.utl.ist.isr.livetrack.server.interfaces.Tracker;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
public class TrackerNode extends AbstractNode {

    private Action[] actions = new Action[]{
        new SystemAction() {

            @Override
            public String getName() {
                return NbBundle.getMessage(this.getClass(), "Tracker_openTrackerWindow");
            }

            @Override
            public HelpCtx getHelpCtx() {
                return null;
            }

            @Override
            public void actionPerformed(ActionEvent ev) {
                TrackerNode tn = Utilities.actionsGlobalContext().lookup(TrackerNode.class);
                TrackerTopComponent.openTracker(tn.server, tn);
            }
        }};
    private LTServer server;
    private Sheet sheet;
    private Sheet.Set set;

    public TrackerNode(LTServer server) {
        super(Children.LEAF, Lookups.singleton(server.getTracker()));
        this.server = server;
        setDisplayName(NbBundle.getMessage(this.getClass(), "TrackerNode_title"));
        init();
    }

    private void init() {
        try {
            sheet = Sheet.createDefault();
            set = Sheet.createPropertiesSet();
            final Tracker obj = getLookup().lookup(Tracker.class);

            addProperties(new PropertySupport.Reflection[]{
                        new PropertySupport.Reflection(obj, boolean.class, "isRunning", null),
                        new PropertySupport.Reflection(obj, int.class, "getBestKHypGenerated", "setBestKHypGenerated"),
                        new PropertySupport.Reflection(obj, int.class, "getFeaturesDifferencePenalization", "setFeaturesDifferencePenalization"),
                        new PropertySupport.Reflection(obj, double.class, "getFeaturesUpdateRate", "setFeaturesUpdateRate"),
                        new PropertySupport.Reflection(obj, int.class, "getGateSize", "setGateSize"),
                        new PropertySupport.Reflection(obj, int.class, "getMaxDepth", "setMaxDepth"),
                        new PropertySupport.Reflection(obj, int.class, "getMaxNumLeaves", "setMaxNumLeaves"),
                        new PropertySupport.Reflection(obj, int.class, "getMaxTimeUndetected", "setMaxTimeUndetected"),
                        new PropertySupport.Reflection(obj, double.class, "getProbChangeZone", "setProbChangeZone"),
                        new PropertySupport.Reflection(obj, double.class, "getProbFalseAlarm", "setProbFalseAlarm"),
                        new PropertySupport.Reflection(obj, double.class, "getProbNewTarget", "setProbNewTarget"),
                        new PropertySupport.Reflection(obj, double.class, "getProbUndetected", "setProbUndetected"),
                        new PropertySupport.Reflection(obj, int.class, "getHistogramMaxDepth", "setHistogramMaxDepth"),
                        new PropertySupport.Reflection(obj, int.class, "getHistogramNBins", "setHistogramNBins"),
                        new PropertySupport.Reflection(obj, long.class, "getStepTime", null),
                        new PropertySupport.Reflection(obj, long.class, "getNSteps", null),
            },
                    new String[]{
                        NbBundle.getMessage(this.getClass(), "TrackerNode_Running"),
                        NbBundle.getMessage(this.getClass(), "TrackerNode_BestKHypGenerated"),
                        NbBundle.getMessage(this.getClass(), "TrackerNode_FeaturesDifferencePenalization"),
                        NbBundle.getMessage(this.getClass(), "TrackerNode_FeaturesUpdateRate"),
                        NbBundle.getMessage(this.getClass(), "TrackerNode_GateSize"),
                        NbBundle.getMessage(this.getClass(), "TrackerNode_MaxDepth"),
                        NbBundle.getMessage(this.getClass(), "TrackerNode_MaxNumLeaves"),
                        NbBundle.getMessage(this.getClass(), "TrackerNode_MaxTimeUndetected"),
                        NbBundle.getMessage(this.getClass(), "TrackerNode_ProbChangeZone"),
                        NbBundle.getMessage(this.getClass(), "TrackerNode_ProbFalseAlarm"),
                        NbBundle.getMessage(this.getClass(), "TrackerNode_ProbNewTarget"),
                        NbBundle.getMessage(this.getClass(), "TrackerNode_ProbUndetected"),
                        NbBundle.getMessage(this.getClass(), "TrackerNode_HistogramMaxDepth"),
                        NbBundle.getMessage(this.getClass(), "TrackerNode_HistogramNBins"),
                        NbBundle.getMessage(this.getClass(), "TrackerNode_StepTime"),
                        NbBundle.getMessage(this.getClass(), "TrackerNode_NSteps"),
                    });

            sheet.put(set);
        } catch (NoSuchMethodException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    private void addProperties(Reflection[] props, String[] names) {
        for (int i = 0; i < props.length; i++) {
            Property property = props[i];
            property.setName(names[i]);
            set.put(property);
        }
    }

    @Override
    protected Sheet createSheet() {
        return sheet;
    }

    @Override
    public Action[] getActions(boolean context) {
        return actions;
    }

    public Image getIcon(int type) {
        return ImageUtilities.loadImage("pt/utl/ist/isr/livetrack/ui/icons/bullseye.gif", true);
    }

    @Override
    public Image getOpenedIcon(int type) {
        return ImageUtilities.loadImage("pt/utl/ist/isr/livetrack/ui/icons/bullseye.gif", true);
    }

    public LTServer getServer() {
        return server;
    }
}
